package chances.epg.navigator.history;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Stack;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;

import chances.epg.session.UserSession;
import chances.epg.session.UserSessionHelper;

public class UserAccessStack implements java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2688282515606771085L;
	
	final static String[] excludeParams = new String[]{"leaveFocusId","return","stb_type","user_id","domain","return","sign","refer"};

	static Log accessStackLog = LogFactory
			.getLog("UserAccessStack");
	public static final String NAME = "USER_ACCESS_STACK";
	private Stack<UserAccessEntry> accessStack;
	private String sessionId  ;
	
	
	private UserAccessStack(String id){
		this.sessionId = id;
		this.accessStack = new Stack<UserAccessEntry>();
		
	}
	public UserAccessEntry getPrivEntry(){
		 int index = this.accessStack.size()-2;
		 if(index>-1){
			 return this.accessStack.get(index);
		 }else{
			 return null;
		 }
	}
	protected int indexOf(String path){
		for(int i=0;i<this.accessStack.size();i++){
			UserAccessEntry entry = this.accessStack.elementAt(i);
			if(path.equals(entry.getPath())){
				return i;
			}
		}
		return -1;
	}
	public UserAccessEntry add(String value) {
		 Assert.isTrue(value!=null);
		 UserAccessEntry entry  = UserAccessEntry.parseUrl(value);
		 pop(entry);
		 accessStackLog.info("add(string):"+entry.getFullPath());
		 accessStack.add(entry);
		 return entry;
	}
	public void pop(UserAccessEntry curEntry) {
		 int idx = this.accessStack.indexOf(curEntry);
		  if(idx>-1){
			 for(int i=0;i<this.accessStack.size()-idx+1;i++){
				   this.accessStack.pop();
				   accessStackLog.debug("pop:" + curEntry.getFullPath());
				   System.out.println("pop:" + curEntry.getFullPath());
			 }
		  }
		
	}

	public UserAccessEntry getBizTopEntry(String bizCode){
		for (UserAccessEntry userAccessEntry : accessStack) {
			String path = userAccessEntry.getPath();
			if((path!=null) && path.contains(bizCode) && !path.contains("/cat/")){
				return userAccessEntry;
			}	
		}
		return null;
		
	}

	public UserAccessEntry  add(HttpServletRequest request){
		   UserAccessEntry entry = createUserAccessEntry(request);
		   pop(entry);
		   accessStackLog.info("add(request):"+entry.getFullPath());
		   accessStack.add(entry);
		   return entry;
	}
	
	/**
	 * 鐢熸垚鏈�鍚庝竴涓〉闈㈢殑URL
	 * @param excludeParams 瑕佹帓闄ょ殑鍙傛暟
	 * @param appendParams 鍛�娣诲姞鐨勫弬鏁�
	 * @return
	 */
	public UserAccessEntry  getLast(){
		if(accessStack.isEmpty()){
			return null;
		}else{
			UserAccessEntry last= accessStack.lastElement();
			return last;
		}
	
		
	}
	
	public String  getPrivPageUrl(String path){
		int idx = 0;
		for (UserAccessEntry entry : accessStack) {
			String aPath  = entry.getPath();
			if(path.equals(aPath)){
				int aIndex = idx -1;
				if(aIndex>0){
					return this.accessStack.get(aIndex).getFullPath();
				}
			}else{
				idx++;
			}
		}
		return null;
	}

	
	public String getSessionId() {
		return sessionId;
	}


	public UserAccessEntry pop(){
		if(!accessStack.isEmpty()){
			return accessStack.pop();
		}else{
			return null;
		}
	}

	public void clear(){
		accessStack.clear();
	}
	
	public static UserAccessStack findUserAccessStack(HttpServletRequest request) {
		UserSession epgUserSession = UserSessionHelper.getUserSession(request);
		if(epgUserSession == null){
			return null;
		}
		UserAccessStack userAccessStack =(UserAccessStack) epgUserSession.getAttribute(NAME);
		
		if (userAccessStack == null) {
			HttpSession session = request.getSession();
			userAccessStack = new UserAccessStack(epgUserSession.getUserId());
			epgUserSession.setAttribute(NAME, userAccessStack);
		}
		return userAccessStack;
	}

	public boolean isEmpty() {
		return this.accessStack.isEmpty();
	}
	
	public  UserAccessEntry createUserAccessEntry(HttpServletRequest request){
		String path = request.getContextPath()+request.getServletPath();
		accessStackLog.info("createUserAccessEntry path:"+request.getContextPath()+","+request.getServletPath());
		@SuppressWarnings("unchecked")
		Enumeration<String> aEnum = request.getParameterNames();
		HashMap<String,String> aParams = new HashMap<String,String>();
		while(aEnum.hasMoreElements()){
			String name = aEnum.nextElement();
			aParams.put(name, request.getParameter(name));
		}
		for (int i = 0; i < excludeParams.length; i++) {
			aParams.remove(excludeParams[i]);
		}
		return new UserAccessEntry(path,aParams);	
	}

	
	
	
}
